package org.han.permission.controller;

import org.han.annotation.RonghuanetPermission;
import org.han.permission.domain.Role;
import org.han.permission.dto.RolePermissionDTO;
import org.han.permission.query.RoleQuery;
import org.han.permission.service.IRoleService;
import org.han.util.AjaxResult;
import org.han.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RonghuanetPermission(name = "角色管理",desc = "角色的增删改查")
@RequestMapping("/role")
public class RoleConntroller {
    @Autowired
    private IRoleService roleService;

    @PutMapping
    @RonghuanetPermission(name = "角色的新增/修改")
    public AjaxResult addorudpate(@RequestBody  Role role){
        try {
            if(Objects.isNull(role.getId())){
                //新增
                roleService.insert(role);
            }else {
                //修改
                roleService.update(role);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("新增/修改失败" + e.getMessage());
        }
    }

    @RonghuanetPermission(name = "角色的删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            roleService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @RonghuanetPermission(name = "角色的根据id查询")
    public AjaxResult selectById(@PathVariable("id")Long id){
        try {
            Role role = roleService.selectById(id);
            return AjaxResult.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }

    @GetMapping
    @RonghuanetPermission(name = "角色的查询所有")
    public AjaxResult selectAll(){
        try {
            List<Role> roles = roleService.selectAll();
            return AjaxResult.me().setData(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }

    @PostMapping
    @RonghuanetPermission(name = "角色的分页查询")
    public AjaxResult pageList(@RequestBody RoleQuery query){
        try {
            PageList pageList = roleService.pageList(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }
    @PatchMapping
    @RonghuanetPermission(name = "角色的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            roleService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }
    @RonghuanetPermission(name = "角色设置权限")
    @PutMapping("/permission")
    public AjaxResult saveRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.saveRolePermission(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("权限设置失败" + e.getMessage());
        }
    }

    @GetMapping("/permissionsns/{roleId}")
    @RonghuanetPermission(name = "获取角色权限")
    public AjaxResult getRolePermissionSns(@PathVariable("roleId")Long roleId){
        try {
            List<String> rolePermissionSns = roleService.getRolePermissionSns(roleId);
            return AjaxResult.me().setData(rolePermissionSns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("角色权限获取失败" + e.getMessage());
        }
    }
}
